package IOTWeb.com.dao.meth;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import IOTWeb.com.dao.ExcepContent;
import IOTWeb.com.utils.ConsJson;
import IOTWeb.com.utils.JDUtil;

public class ExcepContentMeth {
	
    private Connection conn;
	private JDUtil jdUtil = new JDUtil();
	
	public void insertExcep(ExcepContent excepContent) throws IOException, SQLException {
        conn = jdUtil.getConnection("URLDev");
        PreparedStatement ps = conn
                .prepareStatement("insert into excepContent(type, violator, checkor, description, file_id) values (?,?,?,?,?)");
        ps.setString(1, excepContent.getType());
        ps.setString(2, excepContent.getViolator());
        ps.setString(3, excepContent.getCheckor());
        ps.setString(4, excepContent.getDescription());
        ps.setString(5, excepContent.getFile_id());
        ps.executeUpdate();
        System.out.println("异常信息表插入成功");

        ps.clearParameters();
        ps.close();
		conn.close();

	}
	
	public String selectExcep() throws IOException, SQLException {
		String result = "";
		ExcepContent excepContent = new ExcepContent();
		ArrayList<ExcepContent> excepContents = new ArrayList<>();
		conn = jdUtil.getConnection("URLDev");
		Statement statement = conn.createStatement();
		// 要执行的SQL语句
		String sql = "select * from excepContent";
		ResultSet rs = statement.executeQuery(sql);
		while (rs.next()) {
			excepContent.setId(rs.getInt("id"));
			excepContent.setType(rs.getString("type"));
			excepContent.setViolator(rs.getString("violator"));
			excepContent.setCheckor(rs.getString("checkor"));
			excepContent.setDescription(rs.getString("description"));
			excepContent.setFile_id(rs.getString("file_id"));
			excepContent.setRecord_time(rs.getDate("record_time"));
			excepContents.add(excepContent);
		}
		result = ConsJson.Object2Json(excepContents);
		System.out.println(result);
		jdUtil.releaseConnection(conn, statement, rs);
		return result;
	}
	
	public void deletExcep(Integer id) throws IOException, SQLException {
        conn = jdUtil.getConnection("URLDev");
        PreparedStatement ps = conn
                .prepareStatement("delete from excepContent where id = "+id);
        ps.executeUpdate();
	}
	
}
